//
//  TTRecyclingHomeViewCell.m
//  TaoTuo
//
//  Created by LG on 2020/8/26.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingHomeViewCell.h"
#import "LLDateUtilis.h"

@interface TTRecyclingHomeViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UIView *leftTopView;
@property (weak, nonatomic) IBOutlet UILabel *leftTopOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftTopTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *lookLabel;
@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *rightBottomBtn;



@end

@implementation TTRecyclingHomeViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.imgView.layer.cornerRadius = 10;
    self.imgView.layer.masksToBounds = YES;
    
    self.rightBottomBtn.layer.cornerRadius = 11;
    self.rightBottomBtn.layer.borderColor = UIColorHex(AE79EF).CGColor;
    self.rightBottomBtn.layer.borderWidth = 1;
    
    [self layoutIfNeeded];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.leftTopView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    self.leftTopView.layer.mask = layer;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timerNotifyAction) name:@"recyclingHomeCellTimerNotify_key" object:nil];
    
}

- (void)timerNotifyAction
{
    if (self.model.SalesModel == 2) {
        self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", self.model.hour, self.model.minute, self.model.second];
    }
}

- (void)setModel:(TTRecyclingHomeListModel *)model
{
    _model = model;
    if (model.Picture.length > 0) {
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.Picture] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    }
    
    self.leftTopOneLabel.text = model.State_Text;
    self.leftTopTwoLabel.text = @"低价捡漏";
    self.titleLabel.text = model.Title;
    
    if (model.SalesModel == 1) {
        [self xunjiaAction];
    } else if (model.SalesModel == 2) {
        [self jingjiaAction];
    } else {
        [self yikoujiaAction];
    }
    
    
}

// 询价
- (void)xunjiaAction
{
    self.endBtn.hidden = YES;
    self.timeLabel.hidden = YES;
    self.lookLabel.hidden = NO;
    
    NSString *priceStr = [NSString stringWithFormat:@"价格￥%@", self.model.Price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:@"￥"]];
    [attr addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:self.model.Price]];
    
    self.priceLabel.attributedText = attr;
    
    NSString *p = @"";
    if (self.model.ReadingVolume / 10000 > 0) {
        p = [NSString stringWithFormat:@"%.1f人围观", self.model.ReadingVolume / 10000.0];
    } else {
        p = [NSString stringWithFormat:@"%ld人围观", self.model.ReadingVolume];
    }
    self.lookLabel.text = p;
    
    [self.rightBottomBtn setTitle:@"立即询价" forState:UIControlStateNormal];
    
    [self.lookLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.endBtn.mas_bottom);
    }];
    
}


// 竞价
- (void)jingjiaAction
{
    self.endBtn.hidden = NO;
    self.timeLabel.hidden = NO;
    self.lookLabel.hidden = NO;
    
    NSString *priceStr = [NSString stringWithFormat:@"起拍价￥%@", self.model.Price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:@"￥"]];
    [attr addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:self.model.Price]];
    
    self.priceLabel.attributedText = attr;
    
    NSString *p = @"";
    if (self.model.ReadingVolume / 10000 > 0) {
        p = [NSString stringWithFormat:@"%.1f人围观  出价%ld次", self.model.ReadingVolume / 10000.0, self.model.Goods_Offer_Count];
    } else {
        p = [NSString stringWithFormat:@"%ld人围观  出价%ld次", self.model.ReadingVolume, self.model.Goods_Offer_Count];
    }
    self.lookLabel.text = p;
    
    [self.rightBottomBtn setTitle:@"立即出价" forState:UIControlStateNormal];
    
    // 获取当前时间
    NSString *currentDateStr = [LLDateUtilis getCurrentTimeString];
    // 距离开始时间差
    NSDateComponents *diff = [LLDateUtilis compareFromDateString:self.model.StartTime toDateString:currentDateStr deteUnit:Hour | Minute | Second];
    if (diff.second < 0) {
        // 还未开始
        diff = [LLDateUtilis compareFromDateString:currentDateStr toDateString:self.model.StartTime deteUnit:Hour | Minute | Second];
        [self.endBtn setTitle:@"距离开始" forState:UIControlStateNormal];
    } else {
        // 时间差
        diff = [LLDateUtilis compareFromDateString:currentDateStr toDateString:self.model.EndTime deteUnit:Hour | Minute | Second];
        if (diff.second < 0) {
            [self.endBtn setTitle:@"已结束" forState:UIControlStateNormal];
        } else {
            [self.endBtn setTitle:@"距离结束" forState:UIControlStateNormal];
        }
    }
    
    [self timerNotifyAction];
    
    [self.lookLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.endBtn.mas_top).mas_offset(-3);
    }];
    
    
}

// 一口价
- (void)yikoujiaAction
{
    self.endBtn.hidden = YES;
    self.timeLabel.hidden = YES;
    self.lookLabel.hidden = NO;
    
    NSString *priceStr = [NSString stringWithFormat:@"价格￥%@", self.model.Price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:@"￥"]];
    [attr addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : UIColorHex(#F22F3A)} range:[priceStr rangeOfString:self.model.Price]];
    
    self.priceLabel.attributedText = attr;
    
    NSString *p = @"";
    if (self.model.ReadingVolume / 10000 > 0) {
        p = [NSString stringWithFormat:@"%.1f人围观", self.model.ReadingVolume / 10000.0];
    } else {
        p = [NSString stringWithFormat:@"%ld人围观", self.model.ReadingVolume];
    }
    self.lookLabel.text = p;
    
    [self.rightBottomBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    
    
    [self.lookLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.endBtn.mas_bottom);
    }];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


//- (void)handleTimeEventWithComponents:(NSDateComponents *)diff
//{
//    self.model.hour = diff.hour >= 0 ? diff.hour : 0;
//    self.model.minute = diff.minute >= 0 ? diff.minute : 0;
//    self.model.second = diff.second >= 0 ? diff.second : 0;
//    // 总秒数
//    self.model.totalSecond = self.model.hour * 3600 + self.model.minute * 60 + self.model.second;
//
//    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", self.model.hour, self.model.minute, self.model.second];
//    if (self.model.timer == nil) {
//        YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
//        self.model.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:proxy selector:@selector(timerAction) userInfo:nil repeats:YES];
//        [[NSRunLoop currentRunLoop] addTimer:self.model.timer forMode:NSRunLoopCommonModes];
//    }
//
//}
//
//- (void)timerAction
//{
//    self.model.totalSecond--;
//    if (self.model.totalSecond < 0) {
//        self.model.totalSecond = 0;
//    }
//
//    self.model.hour = self.model.totalSecond / 3600;
//    self.model.minute = self.model.totalSecond % 3600 / 60;
//    self.model.second = self.model.totalSecond % 3600 % 60;
//
//    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", self.model.hour, self.model.minute, self.model.second];
//
//}

///// 重用之前清楚计时器
//- (void)prepareForReuse
//{
//    [super prepareForReuse];
//    // 先清除定时器
//    [self.model.timer invalidate];
//    self.model.timer = nil;
//}


@end
